// vuex 状态管理
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	// 数据源
	state: {
		userinfo:	uni.getStorageSync('USERINFO')||{},
		historyLists: uni.getStorageSync("__history") || [],
		islogin: uni.getStorageSync("islogin") || false
	},
	mutations: {
		SET_USER_INFO(state,userInfo)
		{
			state.userinfo=userInfo
		},
		SET_islogin(state,islogin)
		{
			state.islogin=islogin
		},
		
		SET_HISTORY_LISTS(state, history) {
			state.historyLists = history
		},
		CLEAR_HISTORY(state) {
			state.historyLists = []
		},
		CLEAR_ISLOGIN(state)
		{
			state.islogin= false
		},
		CLEAR_USER_INFO(state)
		{
			state.userinfo={}
		}
	},
	actions: {
		
		set_userinfo({commit},userinfo)
		{
			uni.setStorageSync('USERINFO',userinfo)
			commit('SET_USER_INFO',userinfo)
		},
		
		set_islogin({commit},islogin)
		{
			uni.setStorageSync('islogin',islogin)
			commit('SET_islogin',islogin)
		},
		
		set_history({
			commit,
			state
		}, history) {
			let list = state.historyLists
			list.unshift(history)
			uni.setStorageSync('__history', list)
			commit('SET_HISTORY_LISTS', list)
		},
		clearHistory({
			commit
		}) {
			uni.removeStorageSync('__history')
			commit('CLEAR_HISTORY')
		},
		clearislogin({
			commit
		}) {
			uni.removeStorageSync('islogin')
			commit('CLEAR_ISLOGIN')
		},
		clearHistory({
			commit
		}) {
			uni.removeStorageSync('USERINFO')
			commit('CLEAR_USER_INFO')
		},
	}
})

export default store
